﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace YJKPatternSign.StairsRoomSection.Kit.Xdata
{
	internal class SignXdataForReference
	{
		private static Schema CreateSimpleSchema()
		{
			SchemaBuilder schemaBuilder = new SchemaBuilder(SignXdataForReference.m_Guid);
			schemaBuilder.SetReadAccessLevel((AccessLevel)1);
			schemaBuilder.SetSchemaName(SignXdataForReference.m_sSignSchemaName);
			schemaBuilder.AddSimpleField(SignXdataForReference.m_sSignViewId, typeof(ElementId));
			return schemaBuilder.Finish();
		}

		private static bool HasSchema(out Schema schema)
		{
			Schema schema2 = Schema.Lookup(SignXdataForReference.m_Guid);
			schema = schema2;
			return schema2 != null;
		}

		public static void WriteSignComment(Element element, ElementId ViewId)
		{
			Schema schema = null;
			if (!SignXdataForReference.HasSchema(out schema))
			{
				schema = SignXdataForReference.CreateSimpleSchema();
			}
			if (SignXdataForReference.m_SignCommontEnt == null)
			{
				SignXdataForReference.m_SignCommontEnt = new Entity(schema);
			}
			Field field = schema.GetField(SignXdataForReference.m_sSignViewId);
			SignXdataForReference.m_SignCommontEnt.Set<ElementId>(field, ViewId, (DisplayUnitType)(-2));
			element.SetEntity(SignXdataForReference.m_SignCommontEnt);
		}

		public static bool ReadSignCommont(Element element, ref ElementId ViewId)
		{
			Schema schema = null;
			if (SignXdataForReference.HasSchema(out schema))
			{
				Entity entity = element.GetEntity(schema);
				if (entity.IsValid())
				{
					Field field = schema.GetField(SignXdataForReference.m_sSignViewId);
					if (field != null)
					{
						ViewId = entity.Get<ElementId>(field);
					}
					return true;
				}
			}
			return false;
		}

		public static Guid m_Guid = new Guid("41834500-AD61-47B4-B8B1-B4342DEB8EBF");

		public static string m_sSignSchemaName = "SignSchemaName";

		public static string m_sSignViewId = "ViewId";

		public static Entity m_SignCommontEnt = null;
	}
}
